//
//  AppDelegate.m
//  CROSBY
//
//  Created by Harvey on 2017/10/3.
//  Copyright © 2017年 Haley. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabbarController.h"

#import <AVFoundation/AVFoundation.h>
#import <Bugly/Bugly.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self setupLanguage];
    
    // appId:82e8ec0129
    // appkey:03ac8ba3-94d4-46d5-929c-97da3a7b2fa3
    [Bugly startWithAppId:@"82e8ec0129"];
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        //第一次启动
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kRhythmMode];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
    [session setActive:YES error:nil];
    
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    MainTabbarController *mainVC = [[MainTabbarController alloc] init];
    [mainVC setupControllers];
    self.window.rootViewController = mainVC;
    
    [self hl_addNotifications];

    // 测试用
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [BluetoothManager standardManager].peripheralType = BTPeripheralTypeGlowaler;
//        [[NSNotificationCenter defaultCenter] postNotificationName:kConnectedPeriphTypeDidChangeNotification object:@(1)];
//    });
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)hl_addNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pheralTypeDidChange:) name:kConnectedPeriphTypeDidChangeNotification object:nil];
}
    
- (void)setupLanguage
{
    if (![[NSUserDefaults standardUserDefaults] objectForKey:kAppLanguage]) {
        NSArray  *languages = [NSLocale preferredLanguages];
        //获得首选语言版本
        NSString *language = [languages objectAtIndex:0];
        if ([language hasPrefix:@"en"]) {
            //英文
            [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:kAppLanguage];
            [[NSUserDefaults standardUserDefaults] setObject:showText(@"英语") forKey:kChosenLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];//同步
            
        } else if ([language hasPrefix:@"es"]) { // 西班牙语
            [[NSUserDefaults standardUserDefaults] setObject:@"es" forKey:kAppLanguage];
            [[NSUserDefaults standardUserDefaults] setObject:showText(@"西班牙语") forKey:kChosenLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];//同步
        } else if ([language hasPrefix:@"pt-PT"]) { //葡萄牙语
            [[NSUserDefaults standardUserDefaults] setObject:@"pt-PT" forKey:kAppLanguage];
            [[NSUserDefaults standardUserDefaults] setObject:showText(@"葡萄牙语") forKey:kChosenLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];//同步
        } else if ([language hasPrefix:@"de"]) { // 德语
            [[NSUserDefaults standardUserDefaults] setObject:@"de" forKey:kAppLanguage];
            [[NSUserDefaults standardUserDefaults] setObject:showText(@"德语") forKey:kChosenLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];//同步
        } else if ([language hasPrefix:@"zh-Hans"]) { //简体中文
            //简体中文
            [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:kAppLanguage];
            [[NSUserDefaults standardUserDefaults] setObject:showText(@"简体中文") forKey:kChosenLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];//同步
        } else {
            // 俄语
            [[NSUserDefaults standardUserDefaults] setObject:@"ru" forKey:kAppLanguage];
            [[NSUserDefaults standardUserDefaults] setObject:showText(@"俄语") forKey:kChosenLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];//同步
            
        }
    }
}

#pragma mark - notification events
- (void)pheralTypeDidChange:(NSNotification *)notice
{
    BTPeripheralType type = [notice.object integerValue];
    MainTabbarController *mainVC = [[MainTabbarController alloc] initWithType:type];
    [mainVC setupControllersWithType];
    self.window.rootViewController = mainVC;
    
}

@end
